<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.userid" placeholder="用户ID"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select size="small" clearable style="width:200px;" v-model="filters.roleid" placeholder="请选择角色">
            <el-option v-for="(role,index) in roles" :key="index" :label="role.label" :value="role.id"></el-option>
          </el-select>
        
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" type="primary" icon="upload2" @click="exportExcels" v-show="userExport">导出</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" type="primary" icon="plus" v-show="userAdd" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" type="primary" icon="search" v-show="userQuery" v-on:click="getcustoms(true)">查询</el-button>
        </el-form-item>

      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="users" stripe border highlight-current-row :height="winHeight-188" v-loading="listLoading" @selection-change="selsChange" @cell-click="dblclick" style="width: 100%;">
      <!-- <el-table-column type="selection" width="55">
      </el-table-column> -->
      <!-- <el-table-column prop="serialNo" label="序号" width="120" sortable>
      </el-table-column> -->

      <el-table-column prop="userid" label="用户ID" width="120" align="center" sortable>
        <template scope="scope">
          <span style="color:#20a0ff;cursor:pointer;text-decoration:underline;">{{scope.row.userid}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" align="center" width="120">
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="90" align="center" :formatter="formatSex">
      </el-table-column>
      <!-- <el-table-column prop="type" label="用户类型" width="100" align="center" :formatter="formatUserType">
      </el-table-column> -->
      <el-table-column prop="phoneno" label="电话" align="center" width="180">
      </el-table-column>
      <el-table-column prop="mobileno" label="手机号" align="center" width="180">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" align="center">
      </el-table-column>

      <!-- <el-table-column prop="status" label="状态" min-width="120" :formatter="formatStatus" sortable>
      </el-table-column> -->
      <el-table-column label="操作" fixed='right' width="150">
        <template scope="scope">
          <el-button size="small" v-show="userRole" @click="handleEdit(scope.$index, scope.row)">角色</el-button>
          <el-button type="primary" size="small" v-show="buttonShows(scope.$index, scope.row)" @click="handleRecover(scope.$index, scope.row)">恢复</el-button>
          <el-button type="danger" size="small" v-show="buttonShow(scope.$index, scope.row)" @click="handleDel(scope.$index, scope.row)">失效</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->

      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>

    <!--详情-->
    <el-dialog title="详情" v-model="detailFormVisible" :close-on-click-modal="false">
      <el-form :model="detailForm" label-width="120px" :rules="detailFormRules" ref="detailForm">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="用户ID">
              <el-input v-model="detailForm.userid" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="用户名">
              <el-input v-model="detailForm.username" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="机构">
              <el-input v-model="detailForm.branchcode" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="小组">
              <el-input v-model="detailForm.termcode" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="用户角色">
              <el-input v-model="detailForm.clevel" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="性别">
              <el-input v-model="detailForm.sex" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="电话">
              <el-input v-model="detailForm.phoneno" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="手机">
              <el-input v-model="detailForm.mobileno" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="邮箱">
              <el-input v-model="detailForm.email" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="登陆次数">
              <el-input v-model="detailForm.loginnum" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="最近登陆时间">
              <el-input v-model="detailForm.lastlogindate" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="退出时间">
              <el-input v-model="detailForm.logoutdate" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="登陆IP">
              <el-input v-model="detailForm.loginip" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="备注">
              <el-input v-model="detailForm.remarks" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="创建日期">
              <el-input v-model="detailForm.created" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="创建人">
              <el-input v-model="detailForm.creater" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <!-- <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="授权类型">
              <el-input v-model="detailForm.authtype" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="状态">
              <el-select :span="24" v-model="detailForm.status" :readonly="true">
                <el-option label="有效" value="1"></el-option>
                <el-option label="无效" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="注销日期">
              <el-input v-model="detailForm.disableddate" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="状态">
              <el-select :span="24" v-model="detailForm.status" :readonly="true">
                <el-option label="有效" value="1"></el-option>
                <el-option label="无效" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="授权级别">
              <el-input v-model="detailForm.authLevel" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="detailFormVisible = false">取消</el-button>
        <!-- <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button> -->
      </div>
    </el-dialog>

    <!--新增界面-->
    <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
      <el-form label-width="120px" :model="addForm" :rules="addFormRules" ref="addForm">

      <!--   <el-row>
          <el-col :xs="24" :sm="24" :md="24" :lg="20">
            <el-form-item label="用户类型:" prop="type">
              <template>
                <el-radio-group v-model="addForm.type" @change="userTypeChange">
                  <el-radio class="radio" label="0">内部用户</el-radio>
                  <el-radio class="radio" label="1">外部用户</el-radio>
                </el-radio-group>
              </template>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row :gutter="20">      
          <!-- <el-col :xs="24" :sm="20" :md="16" :lg="12" v-show="userNameVisible">
            <el-form-item label="LDAP用户名" prop="userid">
              <el-tooltip class="item" effect="dark" content="请输入LDAP用户ID并点击查询" placement="top">
                <el-input v-model="addForm.userid" placeholder="LDAP用户" auto-complete="off" >
                  <el-button slot="append" @click="inquiry" icon="search"></el-button>
                </el-input>
              </el-tooltip>
            </el-form-item>
          </el-col> -->
          <el-col :xs="24" :sm="20" :md="16" :lg="11" v-show="userMobileVisible">
            <el-form-item label="用户名" prop="userid">
              <el-tooltip class="item" effect="dark" content="请输入用户名(6-16位非中文、非数字)"  placement="top">
                <el-input v-model="addForm.userid" placeholder="用户名(6-16位非中文、非数字)" auto-complete="off" >
                </el-input>
              </el-tooltip>
            </el-form-item>
          </el-col>
        </el-row>    
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="真实姓名" prop="username">
              <el-input v-model="addForm.username" :readonly="userNameVisible" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <!-- <el-form-item label="机构">
                  <el-input v-model="addForm.branchcode"  auto-complete="off"></el-input>
              </el-form-item> -->
            <el-form-item label="密码"  prop="password">
              <el-input v-model="addForm.password" type="password" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="小组">
              <el-input v-model="addForm.termcode" auto-complete="off"  placeholder="(选填)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="用户角色" prop="clevel">
              <!-- <el-input v-model="addForm.clevel"  auto-complete="off"></el-input> -->
              <el-select v-model="addForm.clevel" class="otherWidth" placeholder="请选择">
                <!-- <el-option label="" value="1"></el-option>
                    <el-option label="" value="0"></el-option> -->
                <el-option v-for="(role,index) in roles" :key="index" :label="role.label" :value="role.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="性别">
              <!-- <el-input v-model="addForm.sex"  auto-complete="off"></el-input> -->
              <el-select v-model="addForm.sex" class="otherWidth" placeholder="请选择">
                <el-option label="男" value="M"></el-option>
                <el-option label="女" value="F"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="电话" >
              <el-input v-model="addForm.phoneno" placeholder="(选填)" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="手机" prop="mobileno">
              <el-input v-model="addForm.mobileno" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="邮箱">
              <el-input v-model="addForm.email" placeholder="(选填)" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="登陆次数" v-show="IsShow">
              <el-input v-model="addForm.loginnum" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="最近登陆时间" v-show="IsShow">
              <el-input v-model="addForm.lastlogindate" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="退出时间" v-show="IsShow">
              <el-input v-model="addForm.logoutdate" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="登陆IP" v-show="IsShow">
              <el-input v-model="addForm.loginip" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="创建日期" v-show="IsShow">
              <el-input v-model="addForm.created" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="注销日期" v-show="IsShow">
              <el-input v-model="addForm.disableddate" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :sm="20" :md="16" :lg="11">

            <el-form-item label="状态">
              <el-select class="otherWidth" v-model="addForm.status" placeholder="请选择">
                <el-option label="有效" value="1"></el-option>
                <el-option label="无效" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <!-- <el-form-item label="授权类型" >
                  <el-select v-model="addForm.authtype"  :span="24" placeholder="请选择">
                    <el-option label="" value="1"></el-option>
                    <el-option label="" value="0"></el-option>
                  </el-select>
              </el-form-item> -->
            <el-form-item label="备注">
              <el-input v-model="addForm.remarks" auto-complete="off" placeholder="(选填)"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <!--   <el-col :xs="24" :sm="20" :md="16" :lg="11">

                </el-col> -->
          <el-col :xs="24" :sm="20" :md="16" :lg="11">

          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog title="设置权限" v-model="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="" :inline="true" style="text-align:center;" :rules="editFormRules" ref="editForm">
        <!--<el-form-item style="padding:10px;">
                  <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
                    <el-checkbox v-for="role in roles" :label="role.id">{{role.label}}</el-checkbox>
                  </el-checkbox-group>
              </el-form-item> -->

        <el-form-item label="角色" prop="riskCode">
          <el-select v-model="editForm.roleId" placeholder="角色">
            <el-option v-for="(role,index) in roles" :key="index" :label="role.label" :value="role.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import util from '../../common/js/util'

import commons from '../../common/js/common'
//import NProgress from 'nprogress'
import { mapState, mapActions } from 'vuex'
import { adminList, addAdmin, LdapUser, roleList, getRoleOfUser, setRoleToUser, updateAdminStauts } from '../../api/api';
export default {
  data() {
    var usernameRule = (rule, value, callback) => {
      if (this.userMobileVisible) {
        if (this.addForm.type=='1') {
          if (!value) {
            return callback(new Error('姓名不能为空'));
          } else {
            return callback();
          }
        }else {
            return callback();
          }
      } else{
        return callback();
      }
    };

    var passwordRule = (rule, value, callback) => {
      if (this.userMobileVisible) {
        if (this.addForm.type=='1') {
          if (!value) {
            return callback(new Error('密码不能为空'));
          } else {
            return callback();
          }
        }else {
            return callback();
          }
        } else{
        return callback();
      }
      };
      
  var mobilenoRule = (rule, value, callback) => { 
    if (this.userMobileVisible) {
      if (this.addForm.type=='1') {
        if (!value) {
          return callback(new Error('手机号不能为空'));
        } else {
          return callback();
        }
      }else {
          return callback();
        }
     } else{
        return callback();
      }
    };
    var useridRule = (rule, value, callback) => { 
  
        if (!value) {
          return callback(new Error('用户名不能为空'));
        } else {
          if (!(/^[A-Za-z.@-_]{6,16}$/.test(value))) {
            return callback(new Error('您的用户名格式不正确'));
          }else{
            return callback();
          }  
        }
    };
    return {
      checkedCities: ['上海', '北京'],
      roles: [],
      clevels: [],
      filters: {
        username: '',
        roleid:'',
        userid: ''
      },
      users: [],
      lists: [],
      exportTotal: 0,
      loading: '',
      total: 0,
      page: 1,
      pageSize: 10,
      listLoading: false,
      sels: [],//列表选中列
      userid: '',
      addFormVisible: false,//新增界面是否显示
      editFormVisible: false,//修改界面是否显示
      editLoading: false,
      userNameVisible :false,
      userMobileVisible:true,
      addFormRules: {
        type: [
          { required: true, message: '请选择用户类型', trigger: 'blur' }
        ],
        // userid: [
        //   { required: true, message: '请输入用户名', trigger: 'blur' }
        // ],
         userid: [
          { required: true, validator: useridRule, trigger: 'blur' }
        ],
        mobileno: [{ required: true, validator: mobilenoRule, trigger: 'blur' }],
        username: [{ required: true, validator: usernameRule, trigger: 'blur' }],
        password: [{ required: true, validator: passwordRule, trigger: 'blur' }],
        clevel: [{ required: true, message: '请选择角色', }],
      },
      editFormRules: {
        // name: [
        //   { required: true, message: '请输入姓名', trigger: 'blur' }
        // ]
      },
      //修改界面数据
      editForm: {
        roleId: '',
        userId: ''
      },
      detailFormVisible: false,//新增界面是否显示
      addLoading: false,
      IsShow: false,
      userAdd: false,
      userExport: false,
      userInvalid: false,
      userQuery: false,
      userRecovery: false,
      userRemove: false,
      userRole: false,
      detailFormRules: {
        // name: [
        //   { required: true, message: '请输入姓名', trigger: 'blur' }
        // ]
      },
      //新增界面数据
      addForm: {
        type:'0',
        userid: '',
        username: '',
        password: '',
        branchcode: '',
        deptcode: '',
        termcode: '',
        clevel: '',
        sex: '',
        phoneno: '',
        mobileno: '',
        email: '',
        loginnum: '',
        lastlogindate: '',
        logoutdate: '',
        loginip: '',
        remarks: '',
        created: '',
        creater: '',
        authtype: '',
        disableddate: '',
        status: '',
        authLevel: ''
      },
      //详情界面数据
      detailForm: {
        type:'',
        userid: '',
        username: '',
        password: '',
        branchcode: '',
        deptcode: '',
        termcode: '',
        clevel: '',
        sex: '',
        phoneno: '',
        mobileno: '',
        email: '',
        loginnum: '',
        lastlogindate: '',
        logoutdate: '',
        loginip: '',
        remarks: '',
        created: '',
        creater: '',
        authtype: '',
        disableddate: '',
        status: '',
        authLevel: ''
      }

    }
  },
  computed: mapState([
    'winWidth',
    'winHeight'
  ]),
  methods: {
    // formatStatus :function(row, column){
    //   return row.status == '1' ? '有效' : (row.status == '0' ? '无效' : '');
    // },
    formatSex: function (row, column) {
      if (row.sex) {
          if (row.sex == 'M'||row.sex == '男') {
            return row.sex ='男';
          }else{
            return row.sex ='女';
          }
        }
    },
     formatUserType: function (row, column) {
      if (row.type) {
          if (row.type == '0') {
            return '内部';
          }else{
            return '外部';
          }
        }
    },
    
    userTypeChange: function (value) {
      if (value == 0) {
        this.userNameVisible = true;
        this.userMobileVisible = false;
      } else {
        this.userNameVisible = false;
        this.userMobileVisible = true;
      }
    },
    buttonShow(index, row) {
      if (this.userInvalid) {
        return row.status == 1 ? true : false;
      } else {
        return false;
      }
    },
    buttonShows(index, row) {
      if (this.userRecovery) {
        return row.status == 0 ? true : false;
      } else {
        return false;
      }
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
    },
    inquiry() {
      let para = new FormData();
      para.append("userId", this.addForm.userid);
      LdapUser(para).then((res) => {
        if (res.data.state == 0) {
          this.addForm.username = res.data.result.username;
          this.addForm.password = res.data.result.password;
          this.addForm.mobileno = res.data.result.mobileno;
          this.addForm.creater = res.data.result.creater;
          this.addForm.phoneno = res.data.result.phoneno;
          this.addForm.branchcode = res.data.result.branchcode;
          this.addForm.deptcode = res.data.result.deptcode;
          this.addForm.email = res.data.result.email;
          this.addForm.sex = res.data.result.sex;
          this.addForm.clevel = res.data.result.clevel;
          this.addForm.termcode = res.data.result.termcode;
          this.addForm.remarks = res.data.result.remarks;
        }
        else {
          this.$message({
            message: '未查到该用户的相关信息',
            type: 'error'
          })
        }
      });
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getcustoms(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getcustoms(true);
    },
    //获取用户列表
    getcustoms(bool) {
      // let para = {
      //   pageSize: 10,
      //   pageNumber: this.page,
      //   productCname:'',
      //   riskCode:'',
      //   startDate:'',
      //   endDate:''
      // };
      let para = new FormData();
      let role = new FormData();
      para.append("pageSize", bool ? this.pageSize : this.exportTotal);
      para.append("pageNumber", bool ? this.page : 1);
      para.append("username", this.filters.username.trim());
      para.append("roleid", this.filters.roleid);
      para.append("userid", this.filters.userid.trim());
      this.listLoading = bool;
      //NProgress.start();
      if (!bool) {
        this.loading = this.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }

      adminList(para).then((res) => {
        this.total = res.data.result.totalElements;
        this.exportTotal = res.data.result.totalElements;
        this.lists = res.data.result.content;
        this.listLoading = false;

        if (!bool) {
          this.loading.close();
          this.export2Excel();
        }
        else {
          this.users = res.data.result.content;
        }
        //NProgress.done();
      });
      role.append("pageSize", 100);
      role.append("pageNumber", this.page);
      role.append('type','');
      roleList(role).then((res) => {
        var data = res.data.result.content;
        if (this.roles == "") {
          for (var i in data) {
            this.roles.push({
              id: data[i].roleid,
              label: data[i].rolename
            });
          }
        }
      });
      // }
    },
    //详情
    dblclick(row, column, cell, event) {
      if (column.label == '用户ID') {
        this.detailFormVisible = true;
        this.detailForm = row;
        // this.detailForm.lastlogindate = commons.getDaytime(row.lastlogindate, 1);
        // this.detailForm.logoutdate = commons.getDaytime(row.logoutdate, 1);
        // this.detailForm.created = commons.getDaytime(row.created, 1);
        // this.detailForm.lastlogindate = row.lastlogindate;
        // this.detailForm.logoutdate = row.logoutdate;
        // this.detailForm.created = row.created;
        // this.detailForm.disableddate = commons.getDaytime(row.disableddate, 1);
        this.detailForm.status = row.status;
        this.rowID = row.id;
        this.detailForm.clevel = row.role.rolename;
        if (this.detailForm.sex) {
          if (this.detailForm.sex == 'M'||this.detailForm.sex == '男') {
            this.detailForm.sex = '男';
          }else{
            this.detailForm.sex = '女';
          }
        }

      }
    },
    //恢复
    handleRecover: function (index, row) {
      this.$confirm('确认恢复该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let para = new FormData();
        para.append("userid", row.userid);
        para.append("type", "1");
        updateAdminStauts(para).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '恢复成功',
            type: 'success'
          });
          this.getcustoms(true);
        });
      }).catch(() => {

      });
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认失效该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let para = new FormData();
        para.append("userid", row.userid);
        para.append("type", "0");
        updateAdminStauts(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '失效成功',
            type: 'success'
          });
          this.getcustoms(true);
        });
      }).catch(() => {

      });
    },

    //table导出excel
    exportExcels: function () {
      this.getcustoms(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = ['用户ID', '用户名', '机构', '小组', '性别', '电话', '手机', '邮箱', '登陆次数', '最近登陆时间', '退出时间', '登陆IP', '备注', '创建日期', '创建人', '注销日期', '状态'];
        const filterVal = ['userid', 'username', 'branchcode', 'termcode', 'sex', 'phoneno', 'mobileno', 'email', 'loginnum', 'lastlogindate', 'logoutdate', 'loginip', 'remarks', 'created', 'creater', 'disableddate', 'status'];
        const list = this.lists;
        for (var i in list) {
          list[i].sex = list[i].sex == "M" ? '男' : '女';
          list[i].lastlogindate = commons.getDaytime(list[i].lastlogindate, 1, false);
          list[i].logoutdate = commons.getDaytime(list[i].logoutdate, 1, false);
          list[i].disableddate = commons.getDaytime(list[i].disableddate, 1, false);
          list[i].created = commons.getDaytime(list[i].created, 1, false);
        }
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '用户列表excel');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },

    //显示修改界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      // this.editForm = Object.assign({}, row);
      this.editForm.userId = row.userid;
      let para = new FormData();
      para.append('userId', row.userid);
      getRoleOfUser(para).then((res) => {
        this.editForm.roleId = res.data.result.roleid;
      });
    },
    //显示新增界面
    handleAdd: function () {
      this.addFormVisible = true;
      this.addForm = {
        type:'1',
        userid: '',
        username: '',
        password: '',
        branchcode: '',
        deptcode: '',
        termcode: '',
        clevel: '',
        sex: '',
        phoneno: '',
        mobileno: '',
        email: '',
        loginnum: '',
        lastlogindate: '',
        logoutdate: '',
        loginip: '',
        remarks: '',
        created: '',
        creater: '',
        authtype: '',
        disableddate: '',
        status: '',
        authLevel: ''
      };
      this.addForm.status = '1';
    },
    //新增提交
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            //NProgress.start();
            let para = this.addForm;
            addAdmin(para).then((res) => {
              if (res.data.state == 1) {
                this.$message({
                  message: '该用户已经存在！',
                  type: 'error'
                })
              }
              else {
                this.addLoading = false;
                let paras = new FormData();
                paras.append('userId', this.addForm.userid);
                paras.append('roleId', this.addForm.clevel);
                setRoleToUser(paras).then((res) => {
                  if (res.data.success) {
                    this.editLoading = false;
                  }
                  else {
                    this.$message({
                      message: res.data.result,
                      type: 'error'
                    });
                  }
                }).catch(() => {
                });

                //NProgress.done();
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['addForm'].resetFields();
                this.addFormVisible = false;
                this.getcustoms(true);
              }


            });
          });
        }
      });
    },
    //修改
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = new FormData();
            para.append('userId', this.editForm.userId);
            para.append('roleId', this.editForm.roleId);

            setRoleToUser(para).then((res) => {
              if (res.data.success) {
                this.editLoading = false;
                //NProgress.done();
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['editForm'].resetFields();
                this.editFormVisible = false;
                this.getcustoms(true);
              }
              else {
                this.editLoading = false;
                this.$message({
                  message: res.data.result,
                  type: 'error'
                });
                this.editFormVisible = false;
              }
            }).catch(() => {
              this.editLoading = false;
            });
          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        // let para = { ids: ids };
        let para = new FormData();
        para.append("ids", ids);
        removeAd(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getcustoms(true);
        });
      }).catch(() => {

      });
    },
    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('162'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "userAdd") {
          _this.userAdd = true;
        }
        if (thisData[i] == "userExport") {
          _this.userExport = true;
        }
        if (thisData[i] == "userInvalid") {
          _this.userInvalid = true;
        }
        if (thisData[i] == "userQuery") {
          _this.userQuery = true;
        }
        if (thisData[i] == "userRecovery") {
          _this.userRecovery = true;
        }
        if (thisData[i] == "userRemove") {
          _this.userRemove = true;
        }
        if (thisData[i] == "userRole") {
          _this.userRole = true;
        }
      });
    },
  },
  mounted() {
    this.btnShow();
    this.getcustoms(true);
  }
}

</script>

<style scoped>

</style>